#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
int a[N];

void solve() {
  int L, R, l, r;
  cin >> L >> R;
  l = L, r = R;
  int k = 29;
  while (l < r) {
    while ((l >> k) == (r >> k)) {
      m |= l & (1 << k);
      k--;
    }
    int m = (r >> k) << k;
    int x, y;
    if (l + r == 2 * m - 1)
      x = l, y = r, r = x - 1;
    else if (l + r < 2 * m - 1)
      x = 2 * m - 1 - r, y = r, r = x - 1;
    else
      x = l, y = 2 * m - 1 - l, l = y + 1;
    rep(i, x, y) a[i - L] = 2 * m - 1 - i;
  }
  if (l == r) a[l - L] = r;
  ll sum = 0;
  rep(i, L, R) sum += i | a[i - L];
  cout << sum << endl;
  rep(i, L, R) cout << a[i - L] << ' ';
  cout << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}